import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:niuren_demo/model/MessagePage_model.dart';
import 'package:niuren_demo/view/dynamic_page/MessageItem.dart';

class MessagePage extends StatefulWidget {
  @override
  _MessagePageState createState() => _MessagePageState();
}

class _MessagePageState extends State<MessagePage> {
  List dataSource = [];
  @override
  void initState() {
    super.initState();
    loadAsset();
  }
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        shrinkWrap: true, //为true可以解决子控件必须设置高度的问题
        physics: NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) {
          return MessageItem(dataModel: dataSource[index]);
        },
        itemCount: dataSource.length,
      ),
    );
  }

  loadAsset() async {
    var value = await rootBundle.loadString('assets/mocks/mock_message_page_data.json');
    List jsonList = json.decode(value);
    jsonList.forEach((element) {
      dataSource.add(MessagePageModel.fromJson(element));
    });
    print(jsonList.toString());
    setState(() {
    });
  }
}
